package com.ooqiu.gaming.view.option_item;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ooqiu.gaming.R;
import com.ooqiu.gaming.utils.SizeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Eenie
 * @date 2017/11/23
 */

public class OptionItem extends FrameLayout implements View.OnClickListener {
    @BindView(R.id.img_option_icon)
    ImageView mImgOptionIcon;
    @BindView(R.id.tv_option_name)
    TextView mTvOptionName;
    @BindView(R.id.img_notice_icon)
    ImageView mImgNoticeIcon;
    @BindView(R.id.lay_name)
    LinearLayout mLayName;
    @BindView(R.id.tv_sub)
    TextView mTvSub;
    @BindView(R.id.lay_content)
    RelativeLayout mLayContent;
    @BindView(R.id.img_next)
    ImageView mImgNext;
    private Context mContext;
    private int mChildSpace;
    private Drawable mOptionIcon;
    private Drawable mOptionNextIcon;
    private Drawable mOptionNoticeIcon;

    private CharSequence mOptionName;
    private CharSequence mOptionSubText;


    private boolean mShowIcon;
    private boolean mShowNext;
    private boolean mShowNotice;

    private int mOptionNameStyle;
    private int mOptionSubStyle;


    private View mChildView;

    private OptionItem.OnItemClickListener mOnItemClickListener;


    public OptionItem(@NonNull Context context) {
        this(context, null);
    }

    public OptionItem(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public OptionItem(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init(attrs);
    }


    private void init(@Nullable AttributeSet attrs) {
        LayoutInflater.from(mContext).inflate(R.layout.view_option_item, this);
        ButterKnife.bind(this);
        setOnClickListener(this);
        if (attrs != null) {
            TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.OptionItem);
            mChildSpace = a.getResourceId(R.styleable.OptionItem_option_child_view_space, SizeUtils.dip2px(mContext, 10));
            mOptionIcon = generateIcon(a.getResourceId(R.styleable.OptionItem_option_icon, 0));
            mOptionNextIcon = generateIcon(a.getResourceId(R.styleable.OptionItem_option_next_icon, 0));
            mOptionNoticeIcon = generateIcon(a.getResourceId(R.styleable.OptionItem_option_notice_icon, 0));
            mOptionName = a.getString(R.styleable.OptionItem_option_name);
            mOptionSubText = a.getString(R.styleable.OptionItem_option_sub_text);

            mShowIcon = a.getBoolean(R.styleable.OptionItem_option_show_icon, false);
            mShowNext = a.getBoolean(R.styleable.OptionItem_option_show_next, true);
            mShowNotice = a.getBoolean(R.styleable.OptionItem_option_show_notice, false);

            mOptionNameStyle = a.getResourceId(R.styleable.OptionItem_option_name_text_style, R.style.TopBarMainText_Black);
            mOptionSubStyle = a.getResourceId(R.styleable.OptionItem_option_sub_text_style, R.style.TopBarMinorText_Gay);

            a.recycle();
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mChildView = mTvSub;
        mImgOptionIcon.setBackground(mOptionIcon);
        mImgOptionIcon.setVisibility(mShowIcon ? VISIBLE : GONE);

        mTvOptionName.setText(mOptionName);
        mTvOptionName.setTextAppearance(mContext, mOptionNameStyle);

        mImgNoticeIcon.setImageDrawable(mOptionNoticeIcon);
        mImgNoticeIcon.setVisibility(mShowNotice ? VISIBLE : GONE);

        mImgNext.setImageDrawable(mOptionNextIcon);
        mImgNext.setVisibility(mShowNext ? VISIBLE : GONE);


        mTvSub.setText(mOptionSubText);
        mTvSub.setTextAppearance(mContext, mOptionSubStyle);


        if (getChildCount() > 2) {
            throw new IllegalStateException("just allow one chile view , ok?");
        } else if (getChildCount() == 2) {

            mChildView = getChildAt(1);
            removeView(mChildView);
            mTvSub.setVisibility(GONE);
            mLayContent.addView(mChildView);
        }
    }

    private Drawable generateIcon(int resId) {
        if (resId == 0) {
            return null;
        } else {
            return getResources().getDrawable(resId);
        }
    }

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(this);
        }
    }


    public interface OnItemClickListener {
        void onItemClick(OptionItem item);
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }


    public void showNotice(boolean show) {
        mImgNoticeIcon.setVisibility(show ? VISIBLE : GONE);
    }

    public void hideNotice() {
        mImgNoticeIcon.setVisibility(GONE);
    }


    public <T extends View> T getView(@IdRes int viewId) {
        return this.findViewById(viewId);
    }

    public TextView getSubView() {
        return this.findViewById(R.id.tv_sub);
    }

    public void setSubText(String msg) {
        mTvSub.setText(msg);
    }


}
